package com.example.springbootemployees.controller;

import com.example.springbootemployees.Dao.DepartmentMapper;
import com.example.springbootemployees.Dao.EmployeeMapper;
import com.example.springbootemployees.pojo.Department;
import com.example.springbootemployees.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Collection;

//
//
//import com.example.springbootemployees.Dao.DepementDao;
//import com.example.springbootemployees.Dao.EmployeeDao;
//import com.example.springbootemployees.pojo.Department;
//import com.example.springbootemployees.pojo.Employee;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//
//import javax.servlet.http.HttpSession;
//import java.util.Collection;
//
//@Controller
//public class EmployeeController {
//    @Autowired
//    EmployeeDao employeeDao;
//    @Autowired
//    DepementDao depementDao;
//    @RequestMapping("/emps")
//    public String list(Model model){
//        Collection<Employee> employees=employeeDao.getAllEmployees();
//            model.addAttribute("emps",employees);
//            return "emp/list";
//    }
//    @GetMapping ("/add")
//    public String add(Model model){
//        Collection<Department> departments = depementDao.departments();
//        model.addAttribute("departments",departments);
//        return "emp/add";
//    }
//    @PostMapping("/add")
//    public String add(Employee employee){
//        employeeDao.addEmployee(employee);
//        return "redirect:/emps";
//    }
//    @RequestMapping("/edit/{id}")
//    public String edit(@PathVariable int id,Model model){
//        Employee employeeById = employeeDao.getEmployeeById(id);
//        model.addAttribute("empByID",employeeById);
//        Collection<Department> departments = depementDao.departments();
//        model.addAttribute("departments",departments);
//        return "/emp/edit";
//    }
//    @PostMapping("/edit")
//    public String EditEmp(Employee employee){
//        employeeDao.addEmployee(employee);
//        return "redirect:/emps";
//    }
//    @GetMapping("/delete/{id}")
//    public String delete(@PathVariable Integer id){
//        employeeDao.delete(id);
//        return "redirect:/emps";
//
//    }
//    @RequestMapping("/user/logout")
//    public String logout(HttpSession session){
//        session.invalidate();
//        return "redirect:/index.html";
//
//    }
//
//}
@Controller
public class EmployeeController{
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @RequestMapping("/emps")
    public String list(Model model) {
        Collection<Employee> employees = employeeMapper.getAllEmployees();
        model.addAttribute("emps", employees);
        return "emp/list";
    }
    @GetMapping("/add")
    public String add(Model model){
        Collection<Department> departments = departmentMapper.getAllDepartment();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/add")
    public String add(Employee employee){
        System.out.println(employee.toString());
        System.out.println(employee.getDepartment());
        System.out.println(employee.getEid());
        employee.setEid(employee.getDepartment().getId());
        System.out.println(employee.toString());
        System.out.println("第二个");
        System.out.println(employee.getDepartment());
        System.out.println(employee.getEid());
        employeeMapper.addEmployee(employee);
        return "redirect:/emps";
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        employeeMapper.delEmployee(id);
        return "redirect:/emps";

    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable int id,Model model){
        Employee employeeById = employeeMapper.getEmployeeById(id);
        model.addAttribute("empByID",employeeById);
        Collection<Department> departments = departmentMapper.getAllDepartment();
        model.addAttribute("departments",departments);
        return "emp/edit";
    }
    @PostMapping("/edit")
    public String EditEmp(Employee employee){
        employeeMapper.addEmployee(employee);
        return "redirect:/emps";
    }
        @RequestMapping("/user/logout")
    public String logout(HttpSession session) {
            session.invalidate();
            return "redirect:/index.html";


        }
}